package com.xiaoyu.merchant.ui.fragment.main;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.xiaoyu.commonlib.utils.CommonFragmentPagerAdapter;
import com.xiaoyu.commonlib.utils.ViewPagerUtils;
import com.xiaoyu.merchant.R;
import com.xiaoyu.merchant.ui.fragment.order.OrderFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author jx
 * @date 创建时间 2018/12/28
 * @Description 我的订单
 */
public class MyOrderFragment extends Fragment implements View.OnClickListener{
    private View mView;
    @BindView(R.id.activity_order_linear)
    LinearLayout mBarLinear;
    @BindView(R.id.activity_order_line)
    View mBarline;
    @BindView(R.id.activity_order_viewpager)
    ViewPager mViewPager;
    private CommonFragmentPagerAdapter mPageAdapter;
    private ViewPagerUtils mPagerUtils;
    private List<Fragment> fragments = new ArrayList<>();
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (mView == null) {
            mView = inflater.inflate(R.layout.fragment_my_order, container, false);
            //绑定fragment
            ButterKnife.bind(this, mView);
            initView();
        }
        ViewGroup parent = (ViewGroup) mView.getParent();
        if (parent != null) {
            parent.removeView(mView);
        }
        return mView;

    }

    private void initView() {
        mViewPager.addOnPageChangeListener(pageChangeListener);
        for (int i = 0; i < mBarLinear.getChildCount(); i++) {
            mBarLinear.getChildAt(i).setOnClickListener(this);
        }

        mPagerUtils = new ViewPagerUtils(getActivity(), mBarLinear, mBarline);
        mPagerUtils.initLine(48);
        mPagerUtils.setTextColor("#333333", "#0c93e9");
        mPagerUtils.selectItem(0);

        for (int i = 0; i < 5; i++) {
            fragments.add(new OrderFragment().newInstance("order" + i));
        }

        mPageAdapter = new CommonFragmentPagerAdapter(getFragmentManager(), fragments);
        mViewPager.setAdapter(mPageAdapter);
        mViewPager.setCurrentItem(0);

    }
    @Override
    public void onClick(View v) {
        if (v == mBarLinear.getChildAt(0)) {

            mPagerUtils.selectItem(0);
            mPagerUtils.scrollLineMove(0, 0);
            mViewPager.setCurrentItem(0);
        }
        if (v == mBarLinear.getChildAt(1)) {

            mPagerUtils.selectItem(1);
            mPagerUtils.scrollLineMove(1, 0);
            mViewPager.setCurrentItem(1);
        }
        if (mBarLinear.getChildAt(2) == v) {

            mPagerUtils.selectItem(2);
            mPagerUtils.scrollLineMove(2, 0);
            mViewPager.setCurrentItem(2);
        }
        if (mBarLinear.getChildAt(3) == v) {

            mPagerUtils.selectItem(3);
            mPagerUtils.scrollLineMove(3, 0);
            mViewPager.setCurrentItem(3);
        }
        if (mBarLinear.getChildAt(4) == v) {

            mPagerUtils.selectItem(4);
            mPagerUtils.scrollLineMove(4, 0);
            mViewPager.setCurrentItem(4);
        }
    }

    /**
     * viewPager切换监听
     */
    private ViewPager.OnPageChangeListener pageChangeListener = new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            mPagerUtils.scrollLineMove(position, positionOffset);
        }

        @Override
        public void onPageSelected(int position) {
            mPagerUtils.selectItem(position);
            mViewPager.setCurrentItem(position);
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    };

}
